<?php
/*函数和过滤器
如需过滤变量，请使用下面的过滤器函数之一：
filter_var() - 通过一个指定的过滤器来过滤单一的变量
filter_var_array() - 通过相同的或不同的过滤器来过滤多个变量
filter_input - 获取一个输入变量，并对它进行过滤
filter_input_array - 获取多个输入变量，并通过相同的或不同的过滤器对它们进行过滤*/

/*$int = sda;

if(!filter_var($int, FILTER_VALIDATE_INT))
{
    echo("Integer is not valid");
}
else
{
    echo("Integer is valid");
}*/

//选项
/*$var=255;

$int_options = array(
    "options"=>array
    (
        "min_range"=>0,
        "max_range"=>256
    )
);

if(!filter_var($var, FILTER_VALIDATE_INT, $int_options))
{
    echo("Integer is not valid");
}
else
{
    echo("Integer is valid");
}*/
?>

<html>
    <body>
    <form action="<?php echo $_SERVER["PHP_SELF"];?>" method="get">
        name:<input type="text" name="name">
        age:<input type="text" name="age">
        email:<input type="text" name="email">
        <input type="submit" value="submit">
    </form>
    </body>
</html>
<?php
//验证输入
/*if(!filter_has_var(INPUT_GET, "email"))
{
    echo("Input type does not exist");
}
else
{
    if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL))
    {
        echo "E-Mail is not valid";
    }
    else
    {
        echo "E-Mail is valid";
    }
}
*/

//净化输入
/*if(!filter_has_var(INPUT_POST, "url"))
{
    echo("Input type does not exist");
}
else
{
    $url = filter_input(INPUT_POST, "url", FILTER_SANITIZE_URL);
    echo $url;
}*/

//过滤多个输入
/*$filters = array
(
    "name" => array
    (
        "filter"=>FILTER_SANITIZE_STRING
    ),
    "age" => array
    (
        "filter"=>FILTER_VALIDATE_INT,
        "options"=>array
        (
            "min_range"=>1,
            "max_range"=>120
        )
    ),
    "email"=> FILTER_VALIDATE_EMAIL,
);

$result = filter_input_array(INPUT_GET, $filters);

if (!$result["age"])
{
    echo("Age must be a number between 1 and 120.<br />");
}
elseif(!$result["email"])
{
    echo("E-Mail is not valid.<br />");
}
else
{
    echo("User input is valid");
}*/
?>

<?php
function convertSpace($string)
{
    return str_replace("_", " ", $string);
}

$string = "Peter_is_a_great_guy!";

echo filter_var($string, FILTER_CALLBACK, array("options"=>"convertSpace"));
?>
